package com.qf.day10_4;

import java.util.Scanner;

/**
 * @author wgy
 * @version V1.0
 * @Project day10
 * @Package com.qf.day10_4
 * @Description: ${TODO}
 * @Date 2021/5/31 16:12
 * 银行类
 */
public class Bank {
    /**
     * 用户数组
     */
    private User[] users;
    /**
     * 用户个数
     */
    private int size;
    /**
     * 登录用户
     */
    private User loginUser;

    public Bank(){
        users=new User[100];
        //调用初始化方法
        initial();
    }
    public int getSize() {
        return size;
    }
    /**
     * 初始化用户
     */
    public void initial(){
        User user1=new User("622256789876", "1131125363644", "富康", "123456", "110",1000000);
        User user2=new User("622256789877", "1131125363647", "树桐", "123456", "110",1000000);
        User user3=new User("622256789878", "1131125363649", "张浩", "123456", "110",1000000);
        users[0]=user1;
        users[1]=user2;
        users[2]=user3;
        size=3;
    }

    /**
     * 用户登录
     * @param cardNo 卡号
     * @param password 密码
     * @return
     */
    public boolean login(String cardNo,String password){
        for(int i=0;i<size;i++){
            User u=users[i];
            if(u.getCardNo().equals(cardNo)&&u.getPassword().equals(password)){
                loginUser=u;
                return true;
            }
        }
        return false;
    }

    /**
     * 显示菜单（showMenu）
     */
    public void showMenu(){
        if(loginUser==null){
            System.out.println("请先登录");
            return;//结束方法
        }
        Scanner input=new Scanner(System.in);
        boolean flag=true;
        do{
            System.out.println("===================欢迎进入ATM自动银行服务===================");
            System.out.println("-----1.存款 2.取款 3.转账 4.查询余额 5.修改密码 0.退出------");
            System.out.println("请选择");
            int choice=input.nextInt();
            switch (choice){
                case 1:
                    save();
                    break;
                case 2:
                    withDraw();
                    break;
                case 3:
                    trans();
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 0:
                    flag=false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }

        }while (flag);
        System.out.println("欢迎下次光临");
    }

    /**
     * 存款
     */
    public void save(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入存款金额");
        double m=input.nextDouble();
        if(m>0&&m<=10000){
            double balance=loginUser.getBalance();
            loginUser.setBalance(balance+m);
            System.out.println("存款成功:余额为"+loginUser.getBalance());
        }else{
            System.out.println("输入金额有误");
        }
    }
    /**
     * 取款
     */
    public void withDraw(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入取款金额");
        double m=input.nextDouble();
        if(m>0&&m<=10000){
            double balance=loginUser.getBalance();
            //判断余额是否充足
            if(balance>=m) {
                loginUser.setBalance(balance - m);
                System.out.println("取款成功:余额为" + loginUser.getBalance());
            }else{
                System.out.println("余额不足");
            }
        }else{
            System.out.println("输入金额有误");
        }
    }
    public void trans(){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入对方卡号");
        String toCardNo=input.next();
        System.out.println("请输入转账金额");
        double m=input.nextDouble();
        //(1)判断对方卡号是否和当前卡号相同
        if(loginUser.getCardNo().equals(toCardNo)){
            System.out.println("不能给自己转账");
            return;//结束方法
        }
        //(2)判断对方卡号是否存在
        User toUser=null;//对方账户
        for(int i=0;i<size;i++){
            User u=users[i];
            if(u.getCardNo().equals(toCardNo)){
                toUser=u;
            }
        }
        if(toUser==null){
            System.out.println("对方账户不存在");
            return;
        }
        //(3)转账金额必须大于0 并且小于余额
        double balance=loginUser.getBalance();
        if(m>0&&m<=balance){
            //(4)当前账号扣钱
            loginUser.setBalance(balance-m);
            //(5)对方账号加上
            toUser.setBalance(toUser.getBalance()+m);
            System.out.println("转账成功,余额为:"+loginUser.getBalance());

        }else{
            System.out.println("输入金额有误或余额不足");
        }

    }

}
